package com.ruoyi.sl.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.sl.mapper.SlTeamMemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sl.mapper.SlTeamMapper;
import com.ruoyi.sl.domain.SlTeam;
import com.ruoyi.sl.service.ISlTeamService;

/**
 * 班组Service业务层处理
 * 
 * @author admin
 * @date 2024-06-04
 */
@Service
public class SlTeamServiceImpl implements ISlTeamService 
{
    @Autowired
    private SlTeamMapper slTeamMapper;

    @Autowired
    private SlTeamMemberMapper slTeamMemberMapper;


    /**
     * 查询班组
     * 
     * @param teamId 班组主键
     * @return 班组
     */
    @Override
    public SlTeam selectSlTeamByTeamId(Long teamId)
    {
        return slTeamMapper.selectSlTeamByTeamId(teamId);
    }

    //根据负责人名字查询所在班组
    public SlTeam selectSlTeamByLeaderUser(String leaderUser){
        return slTeamMapper.selectSlTeamByLeaderUser(leaderUser);
    }

    /**
     * 查询班组列表
     * 
     * @param slTeam 班组
     * @return 班组
     */
    @Override
    public List<SlTeam> selectSlTeamList(SlTeam slTeam)
    {
        return slTeamMapper.selectSlTeamList(slTeam);
    }

    /**
     * 新增班组
     * 
     * @param slTeam 班组
     * @return 结果
     */
    @Override
    public int insertSlTeam(SlTeam slTeam)
    {
        slTeam.setCreateTime(DateUtils.getNowDate());
        return slTeamMapper.insertSlTeam(slTeam);
    }

    /**
     * 修改班组
     * 
     * @param slTeam 班组
     * @return 结果
     */
    @Override
    public int updateSlTeam(SlTeam slTeam)
    {
        slTeam.setUpdateTime(DateUtils.getNowDate());
        return slTeamMapper.updateSlTeam(slTeam);
    }

    /**
     * 批量删除班组
     * 
     * @param teamIds 需要删除的班组主键
     * @return 结果
     */
    @Override
    public int deleteSlTeamByTeamIds(Long[] teamIds)
    {
        int row = slTeamMapper.deleteSlTeamByTeamIds(teamIds);

        //删除班组成员
        if(row > 0 ) {
            for (int i = 0; i < teamIds.length; i++) {
                slTeamMemberMapper.deleteSlTeamMemberByTeamId(teamIds[i]);
            }
        }
        return row;
    }

    /**
     * 删除班组信息
     * 
     * @param teamId 班组主键
     * @return 结果
     */
    @Override
    public int deleteSlTeamByTeamId(Long teamId)
    {
        int row = slTeamMapper.deleteSlTeamByTeamId(teamId);
        //删除班组成员
        if(row>0){
            slTeamMemberMapper.deleteSlTeamMemberByTeamId(teamId);
        }
        return row;
    }
}
